/**
 *	@file	telseq_leader.c
 *	@brief	ジムリーダー　会話データ抽選＆再生
 *	@author	Miyuki Iwasawa
 *	@date	09.02.06
 *
 *	ジムリーダーのgmmは全部同じIDテーブルを持つ前提でプログラムを組んでいる
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/pm_rtc.h"
#include "gflib/touchpanel.h"
#include "field/eventflag.h"
#include "field/evwkdef.h"
#include "fielddata/script/saveflag.h"
#include "itemtool/itemsym.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_pg_tel.h"
#include "msgdata/msg_tel_p_gl_hayato.h"

#include "../pm_tel.h"
#include "tel/pmtel_def.h"
#include "tel/pmtel_datman.h"
#include "tel/pmtel_dat_def.h"
#include "../pmtel_seq.h"
#include "telseq_play.h"

#define LEADER_MAX	(1)
#define PROMISE_MAX	(5)

static const u8 DATA_LeaderTbl[LEADER_MAX] = {
 PMTEL_NUMBER_GL_HAYATO,
};

static BOOL sub_IsPromiseFull(PMTEL_DATA_MAN* man,u8 my_id);

/**
 *	@brief	電話　ジムリーダーの会話抽選
 */
u16 PMTelPlayL_Leader(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk)
{
	//ジムバッジ16個集めたか？
	if(MyStatus_GetBadgeCount(man->my_sv) >= BADGE_MAX){
		wk->tmp_flag1 = TRUE;
	}else{
		wk->tmp_flag1 = FALSE;
	}
	//再戦フラグOn?
	wk->tmp_flag2  = PMTelSv_RevengeFlagGet(man->tel_sv,wk->id);

	//着信
	if(wk->is_receive){
		wk->seq_code = TEL_SEQ_LEADER_RECEIVE;
	}else{//ノーマル発信
		wk->seq_code = TEL_SEQ_LEADER_CALL;
	}
	return 0;
}

/**
 *	@brief	ジムリーダーの送信会話シーケンス
 */
int PMTelSeq_TypeLeaderCall(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK *wk = &man->twk;
	switch(wk->seq){
	case 0:
		//タグ展開処理
		PMTel_TagSpreadCommon(man);

		if(wk->pbk->map_id == man->zone_id)	{	//ご近所会話
			wk->seq = 0xFF;
			PMTel_MsgPrintGmm(man,man->m_person,m_hayato_ne);
			return FALSE;
		}
		//挨拶出力
		PMTel_MsgPrintGmm(man,man->m_person,m_hayato_01);
		break;
	case 1:
		if(!PMTel_MsgPrintWait(man)) return FALSE;

		if(!wk->tmp_flag1){	//バッジ16個未満
			PMTel_MsgPrintGmm(man,man->m_person,m_hayato_02);
			wk->seq = 0xFF;
			return FALSE;
		}else if(wk->tmp_flag2){	//再戦フラグON?
			PMTel_MsgPrintGmm(man,man->m_person,m_hayato_08);
			wk->seq = 0xFF;
			return FALSE;
		}else if( (wk->rtc_date.week != wk->pbk->week) ||
				  (wk->time_zone != wk->pbk->time)	){	//また電話してね
			PMTel_MsgPrintGmm(man,man->m_person,m_hayato_03);
			wk->seq = 0xFF;
			return FALSE;
		}
		//再戦しない？
		PMTel_MsgPrintGmm(man,man->m_person,m_hayato_04);
		break;
	case 2:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		//選択肢表示
		PMTel_SelBoxSet(man,SBOX_ID_YESNO);
		break;
	case 3:
		//選択待ち
		{
			u8 msg;
			u32 ret = PMTel_SelBoxMain(man);

			if(ret == SBOX_SELECT_NULL){
				return FALSE;
			}
			PMTel_SelBoxExit(man);

			if(ret != 0){	//No
				PMTel_MsgPrintGmm(man,man->m_person,m_hayato_06);
			}else if(sub_IsPromiseFull(man,wk->id)){	//約束できる？
				PMTel_MsgPrintGmm(man,man->m_person,m_hayato_05);
				//約束
				PMTelSv_RevengeFlagSet(man->tel_sv,wk->id,TRUE);
			}else{
				PMTel_MsgPrintGmm(man,man->m_person,m_hayato_07);
			}
		}
		break;
	default:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		MSGMAN_Delete(man->m_person);
		return TRUE;
	}
	++wk->seq;
	return FALSE;
}

/**
 *	@brief	ジムリーダーの着信会話シーケンス
 */
int PMTelSeq_TypeLeaderReceive(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK *wk = &man->twk;
	switch(wk->seq){
	case 0:
		//タグ展開処理
		PMTel_TagSpreadCommon(man);

		//挨拶出力
		PMTel_MsgPrintGmm(man,man->m_person,m_hayato_10);
		break;
	case 1:
		if(!PMTel_MsgPrintWait(man)) return FALSE;

		if(wk->tmp_flag2){	//再戦フラグON?
			PMTel_MsgPrintGmm(man,man->m_person,m_hayato_11);
		}else{
			PMTel_MsgPrintGmm(man,man->m_person,m_hayato_12_1+gf_rand()%3);
		}
		break;
	default:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		MSGMAN_Delete(man->m_person);
		return TRUE;
	}
	++wk->seq;
	return FALSE;
}

/**
 *	@brief	再戦の約束ができるかどうか？
 */
static BOOL sub_IsPromiseFull(PMTEL_DATA_MAN* man,u8 my_id)
{
	int i,num;

	num = 0;
	for(i = 0;i < LEADER_MAX;i++){
		if(DATA_LeaderTbl[i] == my_id){
			continue;
		}
		if(PMTelSv_RevengeFlagGet(man->tel_sv,DATA_LeaderTbl[i])){
			num++;
		}
	}
	if(num < PROMISE_MAX){
		return TRUE;
	}
	return FALSE;
}